package com.example.demo9.controller;

import com.example.demo9.pojo.Cart;
import com.example.demo9.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class CartController {
    @Autowired
    private CartService cartService;

    @GetMapping("/cha")
    public Map<String, Object> getCart(String userid){
        List<Cart> list = cartService.getCart(userid);
        Map<String, Object> map = new HashMap<>();
        map.put("msg", "查询成功");
        map.put("code", 200);
        map.put("total", list.size());
        map.put("rows", list);
        return map;
    }

    @PostMapping("/tianjia")
    public Map<String, Object> addCart(@RequestBody Cart cart){
        int flag = cartService.addCart(cart);
        Map<String, Object> map = new HashMap<>();
        map.put("msg", "添加成功");
        map.put("code", 200);
        map.put("data", flag);
        return map; // 注意这里返回的是整个Map对象，而不是它的size()
    }
}